package com.gofaraway.dao.mysql.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * mysql 代码生成器
 * </p>
 *
 * @author zhang
 * @since 2022-04-10
 */
public class AutoGeneratorUtil {

    /**
     * dataSourceConfig 配置
     */
    public final static String driver_name = "com.mysql.cj.jdbc.Driver";
    public final static String username = "tms_test";
    public final static String pass = "Tms_test123";
    public final static String url = "jdbc:mysql://rm-1hht058q4779l55jp.mysql.rds.aliyuncs.com:3306/transport_test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai";

    /**
     * packageConfig 配置
     */
    public final static String package_config_parent = "com.gofaraway.dao.mysql";


    /**
     * outFileConfig 输出文件配置
     */
    public final static String out_file_mapper_dir = "/go-far-away-dao/src/main/resources/mapper/mysql/";

    /**
     * outFileConfig 输出文件配置
     */
    public final static String out_file_java_dir = "/go-far-away-dao/src/main/java";

    /**
     * RUN THIS
     */
    public static void main(String[] args) {
        List<String> tableNames = new ArrayList<>();
        tableNames.add("sto_shipment");
        batchCreateTable("trunk", tableNames);
    }

    public static void batchCreateTable(String moduleName, List<String> tableNames) {
        for (String tableName : tableNames) {
            createTable(moduleName, tableName);
        }
    }


    /**
     * 创建表
     */
    public static void createTable(String moduleName, String tableName) {
        // 代码生成器
        AutoGenerator autoGenerator = new AutoGenerator();
        // 全局配置
        GlobalConfig globalConfig = getMyGlobalConfig();
        // 数据源配置
        DataSourceConfig dataSourceConfig = getDataSourceConfig();
        // 包配置
        PackageConfig packageConfig = getPackageConfig(moduleName);
        // 自定义配置
        InjectionConfig cfg = getInjectionConfig(moduleName);
        // 策略配置
        StrategyConfig strategy = getStrategyConfig(moduleName, tableName);
        autoGenerator.setGlobalConfig(globalConfig);
        autoGenerator.setDataSource(dataSourceConfig);
        autoGenerator.setPackageInfo(packageConfig);
        autoGenerator.setCfg(cfg);
        autoGenerator.setStrategy(strategy);
        //不生成xml文件和controller层代码
        autoGenerator.setTemplate(new TemplateConfig().setXml(null).setController(null));
        // 选择 freemarker 引擎需要指定如下加，注意 pom 依赖必须有！
        autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());
        autoGenerator.execute();
    }

    private static StrategyConfig getStrategyConfig(String moduleName, String tableName) {
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setInclude(tableName);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(moduleName + "_");
        return strategy;
    }

    private static InjectionConfig getInjectionConfig(String moduleName) {
        String projectPath = System.getProperty("user.dir");
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        FileOutConfig fileOutConfig = new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + out_file_mapper_dir + moduleName + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        };
        focList.add(fileOutConfig);
        cfg.setFileOutConfigList(focList);
        return cfg;
    }

    private static GlobalConfig getMyGlobalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        globalConfig.setOutputDir(projectPath + out_file_java_dir);
        globalConfig.setAuthor("1065277");
        globalConfig.setOpen(false);
        globalConfig.setDateType(DateType.ONLY_DATE);
        return globalConfig;
    }

    private static PackageConfig getPackageConfig(String moduleName) {
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(package_config_parent);
        packageConfig.setModuleName(moduleName);
        //不需要controller层
        packageConfig.setController("");
        return packageConfig;
    }

    private static DataSourceConfig getDataSourceConfig() {
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setTypeConvert(new MySqlTypeConvert());
        dataSourceConfig.setDriverName(driver_name);
        dataSourceConfig.setUsername(username);
        dataSourceConfig.setPassword(pass);
        dataSourceConfig.setUrl(url);
        return dataSourceConfig;
    }

}